package com.wangzhixuan.apiController;



import java.awt.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wangzhixuan.commons.base.ApiBaseController;
import com.wangzhixuan.commons.constant.ApiCode;
import com.wangzhixuan.model.BaseInfo;
import com.wangzhixuan.model.vo.BaseInfoVo;
import com.wangzhixuan.service.IBaseInfoService;
import com.wangzhixuan.service.IPeopleWorkService;
import com.wangzhixuan.service.IUserService;


/**
 * @description：用户管理
 * @author：zhixuan.wang
 * @date：2015/10/1 14:51
 */
//@CrossOrigin(origins = "*")当前版本1.7 会报错，原因版本太低 http://spring.io/guides/gs/rest-service-cors/
@Controller
@RequestMapping("/api")
public class GroupApiController extends ApiBaseController {
	
    @Autowired
    private IUserService userService;
    
    @Autowired 
    private IBaseInfoService baseInfoService;
    
    @Autowired 
    private IPeopleWorkService peopleWorkService;
	
	@RequestMapping("/group/details")
    @ResponseBody
    public Object updateVsersion(String groupId) {
		if(groupId==null){
			return renderError(ApiCode.CODE_500, "groupId不能为空");
		}
		BaseInfo group = baseInfoService.selectGroup(groupId);
		HashMap<String,Object> map = new HashMap<String,Object>();
		ArrayList<Object> list = new ArrayList<>();
		map.put("groupName", group.getJobName());
		Map<String, Object> adminDetl = userService.slectUserDetl(group.getCreateId());
		map.put("admin",adminDetl);
		String responsibleArr = group.getResponsibleArr();
		String[] users = responsibleArr.split(",");
		for(int i=0;i<users.length;i++){
			Map<String, Object> userDetl = userService.slectUserDetl(Integer.valueOf(users[i]));
			list.add(userDetl);
		}
		map.put("groupMemberArr", list);
        return renderSuccess(ApiCode.CODE_200, "成功",map);
    }
	
	@RequestMapping("/group/list")
    @ResponseBody
    public Object groupList(Integer userId) {
		if(userId==null){
			return renderError(ApiCode.CODE_500, "userId不能为空");
		}
		java.util.List<Integer> list = peopleWorkService.selectGroupUser(userId);
		ArrayList<Object> reslist = new ArrayList<>();
		for(int workId : list){
			BaseInfo baseInfo = baseInfoService.selectById(workId);
			HashMap<String,Object> map = new HashMap<String,Object>();
			map.put("groupId", baseInfo.getGroupId());
			map.put("groupName", baseInfo.getJobName());
			reslist.add(map);
		}
        return renderSuccess(ApiCode.CODE_200, "成功",reslist);
    }
}
